package com.yonyou.component;

import com.intellij.dvcs.repo.VcsRepositoryManager;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.UserDataHolder;
import git4idea.GitUtil;
import git4idea.commands.Git;
import git4idea.repo.GitRepository;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.NlsSafe;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.vcs.log.Hash;
import com.yonyou.common.constants.GitConstants;
import git4idea.GitLocalBranch;
import git4idea.commands.*;
import git4idea.repo.GitRepository;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.models.MergeRequest;
import org.gitlab4j.api.models.MergeRequestParams;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.List;
import java.util.stream.Collectors;

public class MRProcess extends Task.Backgroundable {



    private Project project;
    private String[] selectedValues;
    private String commitIDS;
    private  Git git ;
    private GitRepository repository;
    private java.util.LinkedHashMap linkedHashMap ;
    String text1;
    public MRProcess(java.util.LinkedHashMap linkedHashMap ,GitRepository repository, Git git, @Nullable Project project, @Nls(capitalization = Nls.Capitalization.Title) @NotNull String title , String commitIDS, String text1 , String[] selectedValues) {
        super(project, title);
        this.project = project;
        this.git = git;
        this.selectedValues = selectedValues;
        this.commitIDS = commitIDS;
        this.repository = repository;
        this.linkedHashMap = linkedHashMap;
        this. text1=text1;
    }

    @Override
    public void run(@NotNull ProgressIndicator progressIndicator) {
        StatusBar statusBar = WindowManager.getInstance().getStatusBar(project);
        // 设置进度条为模糊进度条
        progressIndicator.setIndeterminate(true);
        try {
            GitLabApi gitLabApi = new GitLabApi(GitConstants.GITLAB_URL, text1);
            String basePath = repository.getProject().getBasePath();
            String path =basePath.substring(basePath.lastIndexOf("/")+1,basePath.length());
            org.gitlab4j.api.models.Project testProject = gitLabApi.getProjectApi().getProject("CTM",  path);
            // 这里就是我们需要模拟执行我们相关的业务逻辑的部分，此处就使线程睡眠3秒作为模拟


            GitLocalBranch localB = repository.getCurrentBranch();
            String branchN = localB.getName();
            linkedHashMap.remove("master");
            if(selectedValues[0].contains("全选"))
                linkedHashMap.remove("全选");
            else{
                java.util.LinkedHashMap newlinkedHashMap=new java.util.LinkedHashMap();
                for(String a :selectedValues){
                    newlinkedHashMap.put(a,a);
                }
                linkedHashMap=newlinkedHashMap;
            }

            for(Object branchName:linkedHashMap.keySet()){

                MergeRequestParams params = new MergeRequestParams()
                        .withSourceBranch(branchN)
                        .withTargetBranch((String)branchName)
                        .withTitle(commitIDS);
                 gitLabApi.getMergeRequestApi().createMergeRequest(testProject, params);
                com.yonyou.ctm.ctmplugin.utils.NotificationUtil.showNotification(NotificationType.INFORMATION,branchName+"::Merge-request successful!");
            }
            // 开启进度条
            progressIndicator.setIndeterminate(true);
            // 设置进度条为百分百
            progressIndicator.setFraction(1.0);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
